import axios from 'axios'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import {getToken} from "./auth"

const serveUrL = "http://localhost:1337";

const instance  = axios.create({
    baseURL:serveUrL,
    timeout:5000
})
// Add a request interceptor
// 发起网络请求之前执行
instance.interceptors.request.use(
    function (config) {
      // Do something before request is sent
      // 设置请求头token
      config.headers.token = getToken(); // 在请求头中设置token
      NProgress.start();
      return config;
    },
    function (error) {
      // Do something with request error
      return Promise.reject(error);
    }
  );
  
  // Add a response interceptor
  // 网络请求完成之后执行
  instance.interceptors.response.use(
    function (response) {
      // Any status code that lie within the range of 2xx cause this function to trigger
      // Do something with response data
      NProgress.done();
      return response.data;
    },
    function (error) {
      // Any status codes that falls outside the range of 2xx cause this function to trigger
      // Do something with response error
      NProgress.done();
      return Promise.reject(error);
    }
  );
  


// get方法
const get = (url,params)=> instance.get(url,{params});

// post方法
const post = (url,params)=>instance.post(url,params);

// put 方法
const put = (url,params)=>instance.put(url,params);

// 删除方法
const del = (url)=>instance.delete(url);

export {get,post,put,del,serveUrL}